

let app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        type_index:0,
        level_index:0,
        source_index:0,
        sex_index:0,
        sex:['请选择','男','女'],
        cate1_index:0,
        cate2_index:0,
        cate3_index:0,
        stock_index:0,
        departs_index:0
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        let that = this;
        let d = app.globalData;
        this.setData({
            platform:d.platform,    
            //open:open,
            width:d.windowWidth,
            height:d.windowHeight,
            statusBarHeight:d.statusBarHeight,
            navHeight:d.navHeight,
            navWidth:d.navWidth,
            navRight:d.navRight,
            marginTop:d.navTitleTop,
            domain:d.domain,
            uploadUrl:d.uploadUrl,
          })
    },
    goBack(){
        wx.navigateBack();
    },
    goIndex(){
        var  url ='/pages/customer/index';
        console.log(url);
        wx.reLaunch({
          url:url
        })
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

        this.getIndex()
    },
    getIndex(){
       let that = this;
       var open=wx.getStorageSync('open');  
        var user_id = open.user_id;  
       // user_id = 227;   
      wx.request({
        headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
        data: {
          from:0,
          user_id:user_id,
          phone:open.phone  
            
        },
        //method: 'POST',
        dataType: 'json',
        timeout: 30000,
        url: app.globalData.platUrl + 'customer/getCustomerSelect',
        success: (res) => {
    
          console.log(res);
          var data = res.data;
    
         // var data = JSON.parse(data.input);
          console.log(data);
          var level = data.level;
          var levels = [];
          levels.push('请选择');
          level.forEach(element => {
             levels.push(element.name+' '+element.desc)
          });
          var cate = data.cate;
          var cate1 = [];
          var cate1_array = [];
          cate1.push('请选择');
          cate1_array.push('');
          cate.forEach(ele=>{
            if(ele.level == 1){
              cate1.push(ele.title)
              cate1_array.push(ele);
            }
          })
          var stocks = data.stock;
          var stock = [];
          var stock_array = [];
          stock.push('请选择');
          stock_array.push('');
        stocks.forEach(ele=>{
            stock.push(ele.depart.name);
            stock_array.push(ele.depart);
        })
        var departs_array=[];
        var departs_arr = [];
        var departs = data.departs;
        departs_array.push('请选择');
        departs_arr.push(0);
        var sources =[]
        departs.forEach(ele=>{
            departs_array.push(ele.name);
            departs_arr.push(ele.id);
        });
        var source = data.source;
        console.log(source)
        var sources =[]
        var sourcesId=[];
        
        for(var key in source){
            //console.log(key)
            sourcesId.push(key);
            sources.push(source[key])
        }
        console.departs;
          that.setData({
            data:data,
            level:levels,
            cate1:cate1,
            departs:data.departs,
            departs_array:departs_array,
            departs_arr:departs_arr,
            cate1_array:cate1_array,
            stock:stock,
            stock_array,
            sources:sources,
            sourcesId:sourcesId
          })
        }
      
       })
    },
    goto(e){
        console.log(e);
        let url   = e.currentTarget.dataset.url;
        let index = e.currentTarget.dataset.index;
        if(index == '#'){
            wx.navigateTo({
              url:url
            })
        }else{
    
        }
      },
      sexChange(e){
        console.log('----333----');
        console.log(e);
        var index = e.detail.value;
        this.setData({
          sex_index:index
        })
      },
      stockChange(e){
      //  console.log('----333----');
        console.log(e);
        var index = e.detail.value;
        this.setData({
          stock_index:index
        })
      },
      departsChange(e){
        console.log('----departs----');
          
          var index = e.detail.value;
          console.log(index);
          var departs_arr = this.data.departs_arr;
          console.log(departs_arr);
          var depart_id = departs_arr[index];
          console.log(depart_id);
          this.setData({
            departs_index:index,
            departs_id:depart_id
          })
        },
      typeChange(e){
        console.log(e);
        var index = e.detail.value;
        this.setData({
          type_index:index
        })
      },
      levelChange(e){
        console.log(e);
        var index = e.detail.value;
        this.setData({
          level_index:index
        })
      },
      sourceChange(e){
        console.log(e);
        var index = e.detail.value;
        this.setData({
          source_index:index
        })
      },
      cate1Change(e){
        console.log(e);
        var index = e.detail.value;
        this.setData({
          cate1_index:index,
        })
        console.log(index)
        var cate1_array = this.data.cate1_array;
        console.log(cate1_array);
        var cate1_id = cate1_array[index].id;
        var from= cate1_array[index].from;
        console.log('cate1_id=>'+cate1_id);
        console.log('cate1_from=>'+from);
        var cate = this.data.data.cate;
        var cate2 = [];
        var cate2_array = [];
        cate2.push('请选择');
        cate2_array.push('');
        cate.forEach(ele=>{
          if(ele.pid == cate1_id){
            cate2.push(ele.title);
            cate2_array.push(ele)
          }
        })
        this.setData({
          cate2:cate2,
          cate2_array:cate2_array,
          cate1_id:cate1_id,
          cate3_select:0,
          cate2_select:index,
          from:from
        })
    
      },
      cate2Change(e){
        console.log(e);
        var index = e.detail.value;
       
        console.log(index)
        var cate2_array = this.data.cate2_array;
        console.log(cate2_array);
        
        var array =cate2_array[index];
        var cate2_id = array.id 
        var last = array.last;
        if(last == 1){
          this.setData({
            cate2_index:index,
            cate2_id:cate2_id,
            cate3_select:0
          })
        }else{
          console.log('cate2_id=>'+cate2_id);
          var cate = this.data.data.cate;
          var cate3 = [];
          var cate3_array = [];
          cate3.push('请选择');
          cate3_array.push('');
          cate.forEach(ele=>{
            if(ele.pid == cate2_id){
              cate3.push(ele.title);
              cate3_array.push(ele)
            }
          })
          this.setData({
            cate2_index:index,
            cate3:cate3,
            cate3_array:cate3_array,
            cate2_id:cate2_id,
            cate3_select:1
          })
      
        }
       
      },
      cate3Change(e){
        console.log(e);
        var index = e.detail.value;
       
        console.log(index)
        var cate3_array = this.data.cate3_array;
        console.log(cate3_array);
        
        var array =cate3_array[index];
        var cate3_id = array.id 
    
          this.setData({
            cate3_index:index,
            cate3_id:cate3_id,
          
          })
       
      },
     inputPhone(e){
        var phone = e.detail.value;
        const regex = /^[1]{1}\d{10}$/;
        //  return regex.test(str);
        let that = this;
        console.log(e);
        if(phone.length != 11){
            that.showModals('手机号码为11位','#','#');
            return false;
        }else if(regex.test(phone)){
            wx.request({
                headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
                data: {
                  mobile:phone,
                  cate:that.data.cate1_id,
                  from:that.data.from
                },
                //method: 'POST',
                dataType: 'json',
                timeout: 30000,
                url: app.globalData.platUrl + 'customer/checkPhone',
                success: (res) => {
            
                  console.log(res);
                  var data = res.data;
            
                 // var data = JSON.parse(data.input);
                  console.log(data);
                  if(res.statusCode == 200){
                   // console.log(data.url)
                    ///that.showModals(data.msg,data.url,data.index)
                    if(data.status==200){

                    }else{
                        that.showModals(data.msg,data.url,data.index)
                    }
                    
                  }else{
                      that.showModals('服务器错误','#','#');
                  }
                }
              
               })
        }else{
            that.showModals('手机号码错误','#','#');
              return false;
          
        }
     },
 

      onSubmit(e) {
        console.log(`数据：${JSON.stringify(e.detail.value)}`)
        let that = this;
        console.log(e);
        var data= this.data;
        var value = e.detail.value;
       // value.inDate = this.data.inDate;
        var user = wx.getStorageSync('open');
        if(value.name.length ==''){
          wx.showToast({
            type:'fail',
            title:'请填写客户姓名'
          })
          return false;
        }
        const regex = /^[1]{1}\d{10}$/;
        //  return regex.test(str);
        if(value.phone.length != 11){
          wx.showToast({
            type:'fail',
            title:'手机号为11位数'
          })
          return false;
        }else if(regex.test(value.phone)){
      
        }else{
          wx.showToast({
            type:'fail',
            title:'手机号码为11位数字'
          })
          return false;
        }
       // var stock_index= data.stock_index;
        if(value.sex == 0){
          wx.showToast({
            type:'fail',
            title:'请选择客户性别'
          })
         // console.log(stock_index);
          return false;
        }
        if(value.departs == 0){
            wx.showToast({
              type:'fail',
              title:'请选择部门'
            })
            //console.log(stock_index);
            return false;
          }
        if(value.stock == 0){
            wx.showToast({
              type:'fail',
              title:'请选择网点'
            })
            //console.log(stock_index);
            return false;
          }
        if(value.type_id == 0){
          wx.showToast({
            type:'fail',
            title:'请选择客户类型'
          })
         // console.log(stock_index);
          return false;
        }
        if(value.level == 0){
          wx.showToast({
            type:'fail',
            title:'请选择客户级别'
          })
          //console.log(stock_index);
          return false;
        }
        if(value.suorce == 0){
          wx.showToast({
            type:'fail',
            title:'请选择客户来源'
          })
          //console.log(stock_index);
          return false;
        }
       if(value.cate1 == 0){
          wx.showToast({
            type:'fail',
            title:'请选择车型'
          })
          //console.log(stock_index);
          return false;
       }
       let cate3_select = this.data.cate3_select;
       if(value.cate2 == 0){
        wx.showToast({
          type:'fail',
          title:'请选择车型'
        })
        //console.log(stock_index);
        return false;
     }else{
      
       if(cate3_select == 1){
        if(value.cate3 == 0){
          wx.showToast({
            type:'fail',
            content:'请选择车型'
          })
          return false;
        }
       }
     }
       let cate1_array = this.data.cate1_array;
       var cate1 = cate1_array[value.cate1].id;
       let cate2_array = this.data.cate2_array;
       var cate2 = cate2_array[value.cate2].id;
    
       if(cate3_select == 1){
        let cate3_array = this.data.cate3_array;
        var cate3= cate3_array[value.cate3].id;
       }else{
        var  cate3 = 0;
       }
       let stock_array = this.data.stock_array;
    var sourcesId = this.data.sourcesId;
    console.log(value.source)
    var source_id = sourcesId[value.source];
    console.log(sourcesId);
    console.log(source_id)
        wx.request({
          headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
          data: {
            mobile:user.phone,
            stock_id:stock_array[value.stock].id,
            departs_id:data.departs_id,
            user_id:user.user_id,
            name:value.name,
            phone:value.phone,
            sex:value.sex,
            type_id:value.type_id,
            level:value.level,
            source:source_id,
            cate1:cate1,
            cate2:cate2,
            cate3:cate3
            
          },
          //method: 'POST',
          dataType: 'json',
          timeout: 30000,
          url: app.globalData.platUrl + 'customer/add',
          success: (res) => {
      
            console.log(res);
            var data = res.data;
      
           // var data = JSON.parse(data.input);
            console.log(data);
            if(res.statusCode == 200){
             // console.log(data.url)
              that.showModals(data.msg,data.url,data.index)
              
            }else{
                that.showModals('服务器错误','#','#');
            }
          }
        
         })
      
      
      },
      showModals:function(msg,url,index){
        wx.showModal({
          cancelColor: 'cancelColor',
          title:'系统提示',
          content:msg,
          //showCancel:false,
          success:function(e){
           
                if(url=='#'){

                }else{
                    if(index=="#"){
                        wx.navigateTo({
                          url: url,
                        })
                   }
                   if(index=="index"){
                    wx.reLaunch({
                      url: url,
                    })
               }
                }          
                         
          }
          

        })

    },
      goBack(){
        wx.navigateBack();
      },
    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})